home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12147 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  71 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Int to Double, Pl Advise!
  5. Date: Fri, 29 Mar 96 12:38:52 GMT
  6. Organization: none
  7. Message-ID: <828103132snz@genesis.demon.co.uk>
  8. References: <isa5224.544.315B647A@age2.age.uiuc.edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <isa5224.544.315B647A@age2.age.uiuc.edu>
  15.            isa5224@age2.age.uiuc.edu "Irfan S. Ahmad" writes:
  16.  
  17. >Hi:
  18. >
  19. >I am having problems with the following type conversion.  From int to double.  
  20. >If someone can comment:
  21. >
  22. >#define Ng 4
  23. >#define Nr 4
  24. >
  25. >#include <stdio.h>
  26. >#include <math.h>
  27. >#include <malloc.h> 
  28.  
  29. None of these header files are actually needed here. malloc.h is not a
  30. standard header file - for malloc() and related functions you should
  31. include stdlib.h.
  32.  
  33. >int p0[Ng][Nr];
  34. >
  35. >int main(){
  36. >int i,j,k;  
  37. >float x =0;
  38. >float y =0; 
  39. >:
  40. >:
  41. >:
  42. >for (i=1; i<Ng; i++) { 
  43. >        for (j=1; j<Nr; j++) {
  44. >   
  45. >         x = x + ((double) p0[i,j]) / (double)(j*j); }
  46.  
  47. The expression (i,j) evaluates i, discards its result evaluates j and uses
  48. the result as the value of the expression. So in this case p0[i,j] is
  49. equivalent to p0[j] which is an array of 4 ints. As the compiler correctly
  50. diagnoses you can't convert from an array to a double.
  51.  
  52. C doesn't have multi-dimensional arrays although you can build arrays of
  53. arrays. You must dereference them appropriately i.e.
  54.  
  55.   p0[i][j]
  56.  
  57. >}
  58. >
  59. >I get the following compilation error(s) on MS C++ compiler:
  60. >error C2440: 'cast' : cannot convert from 'int [4]' to 'double '
  61.  
  62. Make sure you use a compiler appropriate to the language you are writing in.
  63. Since you posted to comp.lang.c I assume that is C. Most C++ compilers have
  64. an ANSI C mode - make sure you use it to compile C code.
  65.  
  66. -- 
  67. -----------------------------------------
  68. Lawrence Kirby | fred@genesis.demon.co.uk
  69. Wilts, England | 70734.126@compuserve.com
  70. -----------------------------------------
  71.